<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card class="box-card">
        <!-- style="text-align: right; margin-bottom: 20px" -->
        <div style="display: flex; justify-content: flex-end; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermisson(1, null)">添加权限</el-button>
        </div>
        <el-table
          :border="true"
          :data="list"
          row-key="id"
          style="width: 100%"
        >
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type === 1" type="text" size="small" @click="addPermisson(2, scope.row.id)">添加</el-button>
              <el-button type="text" size="small" @click="editPermisson(scope.row.id)">编辑</el-button>
              <el-button type="text" size="small" @click="removePermisson(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog
      title="添加权限"
      :visible.sync="dialogVisible"
      width="45%"
      @close="closeDialog"
    >
      <!-- 表单  -->
      <el-form ref="form" :model="reqParams" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="reqParams.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="reqParams.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="reqParams.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="reqParams.enVisible"
            inactive-text="不启用"
            inactive-value="1"
            active-text="启用"
            active-value="0"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="affirmClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 权限相关接口
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permisson'
// 引入平面转树形
import { treeDiagramList } from '@/utils/index'
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      reqParams: {
        enVisible: '0',
        name: null, // 权限名称
        code: null, // 权限标识
        description: null, // 权限描述
        type: null,
        pid: null
      }
    }
  },
  mounted() {
    this.hGetPermissionList()
  },
  methods: {
    // 获取权限列表
    async hGetPermissionList() {
      const res = await getPermissionList()
      // console.log('权限列表：', res)
      // console.log(treeDiagramList(res))
      this.list = treeDiagramList(res)
    },
    // 添加权限
    // 一级：type：1，pid： null
    // 二级：type：2，pid：当前id
    addPermisson(type, id) {
      this.dialogVisible = true
      this.reqParams.type = type
      this.pid = id
    },
    // 关闭对话框
    closeDialog() {
      this.$refs.form.resetFields()
    },
    // 点击确认按钮
    async affirmClick() {
      if (this.reqParams.id) {
        await updatePermission(this.reqParams)
        this.$message.success('编辑成功')
      } else {
        await addPermission(this.reqParams)
        this.$message.success('添加成功')
      }
      this.hGetPermissionList()
      this.dialogVisible = false
    },
    // 删除权限
    removePermisson(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.hGetPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    },
    // 编辑权限
    async editPermisson(id) {
      this.dialogVisible = true
      const res = await getPermissionDetail(id)
      this.reqParams = res
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
